/**
 * 给所有datagrid的添加列表头的右击菜单；
 * wuyanjun
 * 用法：
 * onHeaderContextMenu: function(e, field){
        e.preventDefault();
        $(this).datagrid('columnMenu').menu('show', {
            left:e.pageX,
            top:e.pageY
        });
    }
 */
(function($){
    function buildMenu(target){
        let state = $(target).data('datagrid');
        if (!state.columnMenu){
            state.columnMenu = $('<div></div>').appendTo('body');
            state.columnMenu.menu({
                onClick: function(item){
                    if (item.iconCls == 'tree-checkbox1'){
                        $(target).datagrid('hideColumn', item.name);
                        $(this).menu('setIcon', {
                            target: item.target,
                            iconCls: 'tree-checkbox0'
                        });
                    } else {
                        $(target).datagrid('showColumn', item.name);
                        $(this).menu('setIcon', {
                            target: item.target,
                            iconCls: 'tree-checkbox1'
                        });
                    }
                }
            })
            let fields = $(target).datagrid('getColumnFields',true).concat($(target).datagrid('getColumnFields',false));
            for(let i=0; i<fields.length; i++){
                let field = fields[i];
                let col = $(target).datagrid('getColumnOption', field);
                let iconClsCheckbox = 1;
                if(col.hidden){ //处理默认是是否隐藏，如果隐藏则复选框不选中
                    iconClsCheckbox = 0;
                }
                state.columnMenu.menu('appendItem', {
                    text: col.title,
                    name: field,
                    iconCls: 'tree-checkbox'+iconClsCheckbox
                });
            }
        }
        return state.columnMenu;
    }
    $.extend($.fn.datagrid.methods, {
        columnMenu: function(jq){
            return buildMenu(jq[0]);
        }
    });
})(jQuery);

/**
 * 最高权限菜单，在任何清空下都会被显示出来。
 * 2018-12-30，wyj
 */
var adminMeun = [{
    id:'adminitratorMenu',
    text: 'WPS考题管理',
    iconCls: 'fa fa-file-word-o',
    state: "open",
    children: [
    //     {
    //     //iconCls: 'icon-ok',
    //     id:'adminitratorMenu-cztgl',
    //     parentNodeName:'WPS操作题管理',
    //     text: 'WPS操作题管理', //<i class="fa fa-cogs" style="font-size:14px;color:green" aria-hidden="true"></i>
    //     attributes:{
    //         "menuURL":"admin/admin_book.html",//admin_manage.html
    //         "remark":100
    //     }
    // }, {
    //      id:'adminitratorMenu-dxtgl1',
    //      parentNodeName:'WPS单选题管理',
    //      text: 'WPS单选题管理', //
    //      selected: true,
    //      attributes:{
    //         "menuURL":"admin/admin_excel.html",
    //         "remark":100
    //      }
    //  },
        {
        id:'multiple_choice_questions2',
        parentNodeName:'单选题管理',
        text: '单选题管理  <i class="fa fa-dot-circle-o" style="font-size:14px;color:green" aria-hidden="true"></i>', //
        selected: true,
        attributes:{
            "menuURL":"admin/multiple_choice_questions2.html",
            "remark":100
        }

    }, {
        id:'multiple_choice_questions',
        parentNodeName:'多选题管理',
        text: '多选题管理  <i class="fa fa-check-square-o" style="font-size:14px;color:green" aria-hidden="true"></i>',
        selected: true,
        attributes:{
            "menuURL":"admin/multiple_choice_questions.html",
            "remark":100
        }

    }, {
         id:'adminitratorMenu-tktgl',
         parentNodeName:'填空题管理',
         text: '填空题管理  <i class="fa fa-edit" style="font-size:14px;color:green" aria-hidden="true"></i>',
         selected: true,
         attributes:{
            "menuURL":"admin/completion.html",
            "remark":100
         }
    }, {
        id:'adminitratorMenu-jdtgl',
        parentNodeName:'解答题管理',
        text: '解答题管理  <i class="fa fa-book" style="font-size:14px;color:green" aria-hidden="true"></i>', //<i class="fa fa-book" style="font-size:14px;color:green" aria-hidden="true"></i>
        selected: true,
        attributes:{
            "menuURL":"admin/answer_questions.html",
            "remark":100
        }
    }, {
        id:'adminitratorMenu-pdtgl',
        parentNodeName:'判断题管理',
        text: '判断题管理  <i class="fa fa-etsy" style="font-size:12px;color:green" aria-hidden="true"></i>',
        selected: true,
        attributes:{
            "menuURL":"admin/judgment_questions.html",
            "remark":100
        }
    }]
},
    {
        id:'weixingmsg',
        text: '考试管理',
        iconCls: 'fa fa-database',
        children: [{
            id: 'userinfo-msg',
            parentNodeName: '用户管理',
            text: '用户管理',
            attributes: {
                "menuURL": "admin/comment.html",
                "remark": 100
            }
        },{
            id: 'kaosheng-msg',
            parentNodeName: '考生管理',
            text: '考生管理',
            attributes: {
                "menuURL": "admin/student_management.html",
                "remark": 100
            }
        },
        //     {
        //     id: 'chouti-wps-msg',
        //     parentNodeName: 'WPS抽题管理',
        //     text: 'WPS抽题管理',
        //     attributes: {
        //         "menuURL": "admin/download.html",
        //         "remark": 100
        //     }
        // },
            {
            id: 'chouti-msg',
            parentNodeName: '抽题管理',
            text: '抽题管理',
            attributes: {
                "menuURL": "admin/association_selected_questions.html",
                "remark": 100
            }
        }]
    },
    {
        id:'jichushezhi',
        text: '基础设置',
        iconCls: 'fa fa-cogs',
        children: [{
            id: 'jichushezhi-structure',
            parentNodeName: '系(专业)管理',
            text: '系(专业)管理',
            attributes: {
                "menuURL": "admin/structure.html",
                "remark": 100
            }
        },{
            id: 'jichushezhi-qita',
            parentNodeName: '其他',
            text: '其他',
            attributes: {
                "menuURL": "admin/admin_manage.html",
                "remark": 100
            }
        }]
    }
];

$(function(){
	$('#top_navigation  > div').mouseover(function(){
		$(this).css("background-color","#fafafa");
	}).mouseout(function(){
		$(this).css("background-color","#fff");
	});

    var data = adminMeun;

    /**
     * 加载左侧的菜单；
     * 点击菜单生成并显示相应的卡片内容
     */
    $('#menu20181026wyj').sidemenu({
        data:data,
        border: 0,
        width:256,
        multiple:false,
        onSelect:function(item){
            let querySidemenuSelected = $('#menu20181026wyj div').filter('.tree-node-selected');
            let id = querySidemenuSelected[0].id;
            let tabsExists = $('#loadingMessageMain_panel').tabs('exists',item.text);//判断是否从在该卡片
            if(tabsExists){
                $('#loadingMessageMain_panel').tabs('select',item.text);
            }else{
                // 处理url有问题的路径，如有问题则转向指定的404页面
                let urlMSG = item.attributes.menuURL;
                if(urlMSG == 'open_target'){
                    window.open("https://mp.weixin.qq.com/");
                    return;
                }
                let strRegex = /([\da-z\.-]+)\.([a-z\.]{2,6})*\/?$/;
                if(!strRegex.test(urlMSG)){
                    urlMSG = './../error/404.html';
                }

                $('#loadingMessageMain_panel').tabs('add',{
                    title: item.text,
                    id:'temp'+id,
                    href: urlMSG,
                    parentNodeID:item.parentNodeName,
                    closable: true,
                    cache:true		//true：第一次打开加载一次内容；false：每次选中都会重新加载里面的内容
                });
            }
        }
    });
    $('.tree').bind("contextmenu",function(e){
        e.preventDefault();
    });

	/**
	 * 卡片的设置和初始化
	 * @returns
	 */
	$('#loadingMessageMain_panel').tabs({
		fit:true,
		border:false,
		plain:true,
		tabHeight:50,
        onSelect: function(title,index){
		    if(index != 0){
                let selectedMenu = $('#menu20181026wyj div').filter('.tree-node-selected');
                let id = selectedMenu[0].id;
                $('#menu20181026wyj #'+id).removeClass('tree-node-selected');
                let tabsObject = $(this).tabs('getTab',index);
                let tabsID = tabsObject.panel('options').id;
                tabsID = tabsID.substring(4);

                var parentNodeID = tabsObject.panel('options').parentNodeID;
                $('#menu20181026wyj > div').accordion('select',parentNodeID);
                $('#'+tabsID).addClass('tree-node-selected');
            }else{
                try{
                    let sTemp = $('#menu20181026wyj div').filter('.tree-node-selected');
                    console.log(sTemp.length);
                    let idd = sTemp[0].id;
                    $('#menu20181026wyj #'+idd).removeClass('tree-node-selected');
                }catch(err){ console.log(err.toLocaleString(),'else'); }
            }
        },
		onContextMenu:function(e, title,index){
			e.preventDefault();
            let item = "";
			$('#loadingMessageMain_panel').tabs("select",title);
			
			if(title=="首页"){
				item = $("#loadingMessageMain_panel").menu("findItem", "关闭");						
				$("#loadingMessageMain_panel").menu("disableItem", item.target);						
			}else{
				item = $("#tabsMenu").menu("findItem", "关闭");						
				$("#tabsMenu").menu("enableItem", item.target);
			}
			$('#tabsMenu').menu('show', {
				left: e.pageX,
				top: e.pageY
			});
		}
	});
});

//选项卡右击菜单上的事件。
var tabsName = 'loadingMessageMain_panel';
$(function(){	
	$('#tabsMenu').menu({ 
		onClick:function(item){
            let selectTabs = $('#'+tabsName).tabs("getSelected");
            let selectTabsTitle = selectTabs.panel('options').title;
			//================
			if(item.id=="close"){
				$('#'+tabsName).tabs("close",selectTabsTitle);
				return false;
			}
			//===============
			if(item.id=="closeAll"){			
				var tabsAll = $('#'+tabsName).tabs("tabs");
				$.each(tabsAll,function(index,msg){	
					$('#'+tabsName).tabs('close', 1);			 	
				});
				return false;
			} 
			//===============
			if(item.id=="closeOther"){
				$('.tabs-inner span').each(function(i,msg){
                    let tabsTitleName = $(msg).text();
					if(tabsTitleName!="首页" && tabsTitleName!=selectTabsTitle){
						$('#'+tabsName).tabs('close',tabsTitleName);
					}	
				});
				return false;
			}
			$('#'+tabsName).tabs({tabPosition:item.id});
			$('#'+tabsName).tabs("select",selectTabsTitle);
		} 
	}); 
});

/**
 * 左侧菜单的折叠和展开
 * 武颜军
 * @returns
 */
function toggle_collapse_expand(){
    let opts = $('#menu20181026wyj').sidemenu('options');
    $('#menu20181026wyj').sidemenu(opts.collapsed ? 'expand' : 'collapse');
    opts = $('#menu20181026wyj').sidemenu('options');
    $('#menu20181026wyj').sidemenu('resize', {
        width: opts.collapsed ? 60 : 254
    })
    let layoutWestWidth = $(".easyui-layout").layout('panel','west').width();
  
    if(layoutWestWidth >= 180){
    	$('#fa-outdent').hide();
    	$('#fa-indent').show();
    	
    	$('#leftTitleshow').hide();
    	layoutWestWidth = 62;
    }else{
    	$('#fa-indent').hide();
    	$('#fa-outdent').show();
    	
    	$('#leftTitleshow').show();
    	layoutWestWidth = 256;
    }
    
	$(".easyui-layout").layout('panel','west').panel("resize",{width:layoutWestWidth});
	$(".easyui-layout").layout("resize");

}
